package com.shen.JUC.CAS;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicStampedReference;

public class refenceTest {
    public static void main(String[] args) throws InterruptedException {
        AtomicStampedReference<Integer> reference = new AtomicStampedReference<>(1, 1);
        new Thread(() -> {
            int stamp = reference.getStamp();//获得版本号
            System.out.println("a1 "+stamp);
        },"a1").start();
        TimeUnit.SECONDS.sleep(3);
        System.out.println(reference.compareAndSet(1, 2, reference.getStamp(), reference.getStamp() + 1));
        System.out.println("a2 "+reference.getStamp());
        System.out.println(reference.compareAndSet(2, 1, reference.getStamp(), reference.getStamp() + 1));
        System.out.println("a2 "+reference.getStamp());
        new Thread(() -> {
            int stamp = reference.getStamp();//获得版本号
            System.out.println("b2 "+stamp);
        },"b2").start();
    }
}
